package com.whmskj.xjlhsc.mobie.Scheduling.controller;


import com.whmskj.xjlhbc.core.AjaxResult;
import com.whmskj.xjlhbc.core.Page;
import com.whmskj.xjlhbc.domain.DictionaryProcess;
import com.whmskj.xjlhbc.dto.JcQueryDto;
import com.whmskj.xjlhbc.vo.ApproveRequest;
import com.whmskj.xjlhsc.controller.vo.Result;
import com.whmskj.xjlhbc.domain.Yg01employeefiles;
import com.whmskj.xjlhsc.mobie.Scheduling.Dto.RecordsOfEmployeeAwardsAndSancti;
import com.whmskj.xjlhsc.mobie.Scheduling.Service.RecordsOfEmployeeAwardsAndSanctiService;
import com.whmskj.xjlhbc.service.Yg01employeefilesService;
import com.whmskj.xjlhsc.mobie.equipmentManage.service.DictionaryProcessService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Author: Sakura
 * @Date: 2025/8/30 10:25
 * @Version: v1.0.0
 * @Description: TODO
 **/
// 奖惩记录
@RestController
@RequestMapping("/mob/jc")
public class RecordsOfEmployeeAwardsAndSanctiController  {
    @Autowired
    private RecordsOfEmployeeAwardsAndSanctiService service;
    @Autowired
    private Yg01employeefilesService yg01employeefilesService;

    @Autowired
    private DictionaryProcessService dictionaryProcessService;

    @GetMapping("/getAwardsAndSanctiReason")
    public Result getAwardsAndSanctiReason(@RequestParam Integer type) {
     List<DictionaryProcess> list = dictionaryProcessService.getAwardsAndSanctiReason(type);
        return Result.success(list);
    }


    @GetMapping("/getEmpList")
    @Operation(description = "获取可以奖惩的员工列表")
    public Result getEmpList(@RequestParam(required = false) String name){
      List<Yg01employeefiles> list = yg01employeefilesService.getEmpList(name);
      return Result.success(list);
    }
    /**
     * 奖惩记录列表
     */
    @PostMapping("/list")
    @Operation(description = "列表")
    @ApiResponse(content = @Content(
            schema = @Schema(anyOf = {AjaxResult.class, RecordsOfEmployeeAwardsAndSancti.class})
    ))
    public AjaxResult list(@RequestBody JcQueryDto param) {
        List<RecordsOfEmployeeAwardsAndSancti> list = service.queryList(param);
        return Page.getDataTable(list);
    }

    /**
     * id查详情
     */
    @GetMapping("/getInfo/{id}")
    @Operation(description = "详情")
    @ApiResponse(content = @Content(
            schema = @Schema(anyOf = {AjaxResult.class, RecordsOfEmployeeAwardsAndSancti.class})
    ))
    public AjaxResult getInfo(@PathVariable("id") String id) {
        RecordsOfEmployeeAwardsAndSancti info = service.getByIdWithUserInfo(id);
        if (info == null) {
            return AjaxResult.error("记录不存在");
        }
        return AjaxResult.success(info);
    }

    /**
     * 新增
     */
    @PostMapping("/save")
    @Operation(description = "新增")
    public AjaxResult save(@RequestBody RecordsOfEmployeeAwardsAndSancti body) {
        boolean save = service.add(body);
        return AjaxResult.success(save);
    }

    /**
     * 编辑
     */
    @PostMapping("/update")
    @Operation(description = "编辑")
    public AjaxResult update(@RequestBody RecordsOfEmployeeAwardsAndSancti body) {
        boolean save = service.updateRecord(body);
        return AjaxResult.success(save);
    }

    /**
     * 获取奖惩记录的所有历史版本
     */
    @GetMapping("/history/{id}")
    @Operation(description = "获取奖惩记录的所有历史版本")
    @ApiResponse(content = @Content(
            schema = @Schema(anyOf = {AjaxResult.class, RecordsOfEmployeeAwardsAndSancti.class})
    ))
    public AjaxResult getHistoryVersions(@PathVariable("id") String id) {
        RecordsOfEmployeeAwardsAndSancti historyVersions = service.getHistoryVersions(id);
        return AjaxResult.success(historyVersions);
    }

    /**
     * 删除
     */
    @GetMapping("/remove/{id}")
    @Operation(description = "删除")
    public AjaxResult remove(@PathVariable(value = "id") String id) {
        boolean save = service.removeRecord(id);
        return AjaxResult.success(save);
    }

    /**
     * 审核奖惩单
     */
    @PostMapping("/approve")
    @Operation(description = "审核奖惩单（支持单个或批量）")
    public AjaxResult approve(@RequestBody ApproveRequest request) {
        List<String> ids = request.getIds();
        if (ids == null || ids.isEmpty()) {
            return AjaxResult.error("请选择要审核的奖惩单");
        }
        boolean success = service.approve(ids);
        return success ? AjaxResult.success("审核成功") : AjaxResult.error("审核失败");
    }

    /**
     * 作废奖惩单
     */
    @PostMapping("/invalidate")
    @Operation(description = "作废奖惩单（支持单个或批量）")
    public AjaxResult invalidate(@RequestBody ApproveRequest request) {
        List<String> ids = request.getIds();
        if (ids == null || ids.isEmpty()) {
            return AjaxResult.error("请选择要作废的奖惩单");
        }
        boolean success = service.invalidate(ids);
        return success ? AjaxResult.success("作废成功") : AjaxResult.error("作废失败");
    }

    /**
     * 当前登录人奖惩记录统计
     */
    @GetMapping("/statistics")
    @Operation(description = "当前登录人奖惩记录统计")
    public AjaxResult getStatistics() {
        try {
            Map<String, Object> statistics = service.getCurrentUserStatistics();
            return AjaxResult.success("查询成功", statistics);
        } catch (Exception e) {
            return AjaxResult.error("查询失败: " + e.getMessage());
        }
    }

    /**
     * 奖惩统计 - 总体统计
     */
    @GetMapping("/statistics/overview")
    @Operation(description = "奖惩统计 - 总体统计")
    public AjaxResult getStatisticsOverview(
            @RequestParam(required = false) String startDate,
            @RequestParam(required = false) String endDate) {
        try {
            Map<String, Object> overview = service.getStatisticsOverview(startDate, endDate);
            return AjaxResult.success("查询成功", overview);
        } catch (Exception e) {
            return AjaxResult.error("查询失败: " + e.getMessage());
        }
    }

    /**
     * 奖惩统计 - 车间统计
     */
    @GetMapping("/statistics/workshop")
    @Operation(description = "奖惩统计 - 车间统计")
    public AjaxResult getWorkshopStatistics(
            @RequestParam(required = false) String startDate,
            @RequestParam(required = false) String endDate) {
        try {
            List<Map<String, Object>> workshopStats = service.getWorkshopStatistics(startDate, endDate);
            return AjaxResult.success("查询成功", workshopStats);
        } catch (Exception e) {
            return AjaxResult.error("查询失败: " + e.getMessage());
        }
    }

    /**
     * 奖惩统计 - 班组统计
     */
    @GetMapping("/statistics/team")
    @Operation(description = "奖惩统计 - 班组统计")
    public AjaxResult getTeamStatistics(
            @RequestParam(required = false) String startDate,
            @RequestParam(required = false) String endDate) {
        try {
            List<Map<String, Object>> teamStats = service.getTeamStatistics(startDate, endDate);
            return AjaxResult.success("查询成功", teamStats);
        } catch (Exception e) {
            return AjaxResult.error("查询失败: " + e.getMessage());
        }
    }

    /**
     * 奖惩统计 - 员工统计
     */
    @GetMapping("/statistics/employee")
    @Operation(description = "奖惩统计 - 员工统计")
    public AjaxResult getEmployeeStatistics(
            @RequestParam(required = false) String startDate,
            @RequestParam(required = false) String endDate) {
        try {
            List<Map<String, Object>> employeeStats = service.getEmployeeStatistics(startDate, endDate);
            return AjaxResult.success("查询成功", employeeStats);
        } catch (Exception e) {
            return AjaxResult.error("查询失败: " + e.getMessage());
        }
    }

}
